package com.come4loves.core.comunicate.router;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.come4loves.core.comunicate.router.annotation.Path;

public abstract class HttpRouterHandler {
	protected Logger LOGGER = LoggerFactory.getLogger(getClass());
	
	/**
	 * 方法存储
	 */
	private static Map<String, Method> METHOD_MAPS = new HashMap<String, Method>();
	private static Map<String, HttpRouterHandler> HANDLER_MAPS = new HashMap<String, HttpRouterHandler>();
	
	@PostConstruct
	private void init() {
		Method[] mList = this.getClass().getDeclaredMethods();
		for (Method m : mList) {
			Path p = m.getAnnotation(Path.class);
			if (p != null) {
				if (METHOD_MAPS.containsKey(p.value())) {
					throw new RuntimeException(String.format("cmd annontation:[%d] duplicated key", p.value()));
				}
				METHOD_MAPS.put(p.value(), m);
				HANDLER_MAPS.put(p.value(), this);

			}
		}
	}
	
	public static Method getMethod(String path) {
		return METHOD_MAPS.get(path);
	}
	public static HttpRouterHandler getHandler(String path) {
		return HANDLER_MAPS.get(path);
	}
}
